from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .models import User, Role, Permission, Menu


@admin.register(Menu)
class MenuAdmin(admin.ModelAdmin):
    list_display = ('name', 'url', 'parent', 'order', 'is_active')
    list_filter = ('is_active',)
    search_fields = ('name',)
    ordering = ('order',)


@admin.register(Permission)
class PermissionAdmin(admin.ModelAdmin):
    list_display = ('name', 'url_name', 'menu')
    search_fields = ('name', 'url_name')


@admin.register(Role)
class RoleAdmin(admin.ModelAdmin):
    list_display = ('name', 'description')
    search_fields = ('name',)
    filter_horizontal = ('permissions', 'menus')


class UserAdmin(BaseUserAdmin):
    list_display = ('username', 'nickname', 'email', 'phone', 'is_staff', 'is_active')
    search_fields = ('username', 'nickname', 'email', 'phone')
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        ('个人信息', {'fields': ('first_name', 'last_name', 'nickname', 'email', 'phone', 'avatar')}),
        ('权限', {'fields': ('is_active', 'is_staff', 'is_superuser', 'roles', 'groups', 'user_permissions')}),
        ('重要日期', {'fields': ('last_login', 'date_joined')}),
    )
    filter_horizontal = ('roles', 'groups', 'user_permissions')


# 注册User模型
admin.site.register(User, UserAdmin)

# Register your models here.
